package com.digital.welcome.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.digital.welcome.entity.Dorm;
import com.digital.welcome.entity.User;
import com.digital.welcome.entity.UserDorm;
import com.digital.welcome.mapper.DormMapper;
import com.digital.welcome.mapper.UserCollegeMapper;
import com.digital.welcome.mapper.UserDormMapper;
import com.digital.welcome.mapper.UserMapper;
import com.digital.welcome.response.Result;
import com.digital.welcome.service.DormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;


@Service
public class DormServiceImpl extends ServiceImpl<DormMapper, Dorm> implements DormService {


    @Autowired
    private DormMapper dormMapper;

    @Autowired
    private UserCollegeMapper userCollegeMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserDormMapper userDormMapper;


    @Override
    public String selectDormNumber(String userName) {
        User user = userMapper.getUserByUsername(userName);
        String sex = user.getSex();
        String majorClassName = userCollegeMapper.selectMajorClassNameByUserName(userName);
        //  List<String> dormNumber = dormMapper.selectDormNumber(userName, majorClassName,sex);
        List<Dorm> list = dormMapper.selectDormNumberAndRegion(userName, majorClassName, sex);
        return JSON.toJSONString(new Result<>().setCode(200).setMessage("查询成功").setData(list));
    }

    @Override
    public String getBedNumber(String region, String dormNumber) {
        List<Dorm> bedNumbers = dormMapper.selectAllDormIdByChose(region, dormNumber);
        return JSON.toJSONString(new Result<>().setCode(200).setMessage("查询成功").setData(bedNumbers));
    }

    @Override
    public String insertDormByUser(String userName, String dormId) {
        dormMapper.updateChoose(dormId);
        boolean res = dormMapper.insertDormByUser(userName, dormId);
        return JSON.toJSONString(new Result<>().setCode(200).setMessage("选择成功"));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String deleteBedInformation(String userName) {
        UserDorm userDorm = userDormMapper.selectDormIdByUserName(userName);
        dormMapper.setChooseNull(userDorm.getDormId());
        boolean res = userDormMapper.deleteDormByUserName(userName);
        return JSON.toJSONString(new Result<>().setCode(200).setMessage("删除成功"));
    }

    @Override
    public String selectDormByUserName(String userName) {
        Dorm dorm = dormMapper.selectDormByUserName(userName);
        if (dorm != null) {
            return JSON.toJSONString(new Result<>().setCode(200).setMessage("查询成功").setData(dorm));
        } else {
            return JSON.toJSONString(new Result<>().setCode(400).setMessage("你还未选择寝室"));
        }

    }

    @Override
    public String selectNumber() {
        Integer count = null;
        QueryWrapper<UserDorm> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNotNull("dorm_id");
        count = userDormMapper.selectCount(queryWrapper);
        //入住人数
        Integer count1 = null;
        QueryWrapper<User> queryWrapper1 = new QueryWrapper<>();
        queryWrapper.isNotNull("category");
        count1 = userMapper.selectCount(queryWrapper1) - count;
        //总
        Integer count2 = count1 - count;
        //未入住
        HashMap<String, Object> map = new HashMap();
        map.put("is", count);
        map.put("no", count2);
        return JSON.toJSONString(new Result<>().setCode(200).setMessage("成功").setData(map));
    }
}
